﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic.Remind;
using EasyStudy.Properties;
using EasyStudy.UI.ProjectUpdate;
using EasyStudy.Logic.Email;
using ComponentFactory.Krypton.Toolkit;
using System.Threading;
using EasyStudy.tools;

namespace EasyStudy.UI
{
    public partial class FrmSimple : KryptonForm
    {
        public static RemindCheck checker; 
        //private ComponentFactory.Krypton.Toolkit.KryptonManager kryptonManager;

        public FrmSimple()
        {
            InitializeComponent();

            //this.kryptonManager = new ComponentFactory.Krypton.Toolkit.KryptonManager(this.components);
            //this.kryptonManager.GlobalPaletteMode = ComponentFactory.Krypton.Toolkit.PaletteModeManager.Office2010Blue;

            // 显示数据库名
            this.Text += "\t" + Configuration.GetInstance().DbFileName;
            this.Text = GlobalSettings.title + " - " + this.Text;

            this.kryptonContextMenuItem2.Click += new EventHandler(kryptonContextMenuItem2_Click);
            this.kryptonContextMenuItem3.Click += new EventHandler(kryptonContextMenuItem3_Click);
            this.kryptonContextMenuItem4.Click += new EventHandler(kryptonContextMenuItem4_Click);

            Icon = Resources.task1;
            timerCheck.Interval = 300000;
            timerCheck.Start();
            checker = new RemindCheck();

            GlobalSettings.AllowPopupValueChanged += new EventHandler(GlobalSettings_AllowPopupValueChanged);
        }

        // 帮助
        void kryptonContextMenuItem4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
            Info.FileName = Environment.CurrentDirectory + "\\help.htm";
            Info.WorkingDirectory = Environment.CurrentDirectory;
            Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;

            try
            {
                System.Diagnostics.Process.Start(Info);
            }
            catch (System.ComponentModel.Win32Exception)
            {
                MessageBox.Show("打开帮助文档失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        // 任务数据分析
        void kryptonContextMenuItem3_Click(object sender, EventArgs e)
        {
            new FrmDataStatistics().Show();
        }

        // 项目总览
        void kryptonContextMenuItem2_Click(object sender, EventArgs e)
        {
            new FrmTaskSummary().Show();
        }

        void GlobalSettings_AllowPopupValueChanged(object sender, EventArgs e)
        {
            this.chkAllowPopup.Checked = (bool)sender;
        }

        private void timerCheck_Tick(object sender, EventArgs e)
        {
            checker.Check();
            // 备份数据库
            DBAutoBackup.StartBackup();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmAddTask formAddTask = new FrmAddTask();
            formAddTask.Visible = true;
        }

        private void btnTodayTasks_Click(object sender, EventArgs e)
        {
            FrmShowTodayTasks frmShowTodayTasks = new FrmShowTodayTasks();
            frmShowTodayTasks.Show(this) ;
        }

        private void btnUnfinished_Click(object sender, EventArgs e)
        {
            FrmShowUnfinishedTasks frmShowUnfinishedTasks = new FrmShowUnfinishedTasks();
            frmShowUnfinishedTasks.Show(this);
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            new FrmProjUpdateMain().ShowDialog();
        }

        private void btnSendEmail_Click(object sender, EventArgs e)
        {
            FrmSendEmail frmSendEmail = new FrmSendEmail();
            frmSendEmail.Show();
        }

        private void FrmSimple_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void FrmSimple_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您今天的任务已经完成了吗？", 
                    "确认退出", 
                    MessageBoxButtons.OKCancel, 
                    MessageBoxIcon.Question, 
                    MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                Configuration.GetInstance().setSoundsEnable(GlobalSettings.SoundOn);
                this.notifyIcon.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void 新的任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAddTask formAddTask = new FrmAddTask();
            formAddTask.Visible = true;
        }

        private void 今日任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmShowTodayTasks frmShowTodayTasks = new FrmShowTodayTasks();
            frmShowTodayTasks.Show(this);
        }

        private void 过去未完成的任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmShowUnfinishedTasks frmShowUnfinishedTasks = new FrmShowUnfinishedTasks();
            frmShowUnfinishedTasks.Show(this);
        }

        private void 发送邮件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSendEmail frmSendEmail = new FrmSendEmail();
            frmSendEmail.Show();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            checker.Check();            
            // 备份数据库
            DBAutoBackup.StartBackup();
        }

        private void kryptonButton5_Click(object sender, EventArgs e)
        {
            
        }

        private void btnDataStatistics_Click(object sender, EventArgs e)
        {
            
        }

        private void kryptonButton5_Click_1(object sender, EventArgs e)
        {

        }

        private void chkAllowPopup_CheckedChanged(object sender, EventArgs e)
        {
            GlobalSettings.AllowPopup = this.chkAllowPopup.Checked;
        }

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {

        }

        private void kryptonLinkLabel4_LinkClicked(object sender, EventArgs e)
        {
            moreMenu.Show(sender);
        }
    }
}
